package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import yja.gzw.app.R
import yja.gzw.app.adapter.DiminAdapter
import yja.gzw.app.adapter.FabuAdapter
import yja.gzw.app.adapter.TuijianAdapter
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityFabaBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.internal.notifyAll
import java.io.File

class FabaActivity :BaseActivity<Mainviewmodel, ActivityFabaBinding>(R.layout.activity_faba),
    FabuAdapter.OnItemClickListener{
    private lateinit var fabiada: FabuAdapter
   public var list= mutableListOf<ZaoShuju>(
        ZaoShuju(2,""),

    )
    var file: File?=null

    override fun onResume() {
        super.onResume()

        binding.dimingzhanshi.text=UserData.diming

    }
    override fun initView() {
        binding.fanhui.setOnClickListener {
            finish()

        }



        binding.review.layoutManager=
            GridLayoutManager(this, 3)
        fabiada= FabuAdapter(this,list,this)
        binding.review.adapter= fabiada


        binding.ditu.setOnClickListener {
            var intent=Intent(this,MapsActivity::class.java)
            startActivity(intent)

        }

        binding.fabuanniu.setOnClickListener {

            var titles=binding.tishi.text.toString()
            var contents=binding.shurukuang.text.toString()
            var imagess=""
            for (zaoShuju in list) {

                imagess=imagess+zaoShuju.tupian+","
            }
            var addresss=binding.dimingzhanshi.text.toString()


            viewmodel.onfabudtwang(UserData.token,mapOf("title" to titles,"content" to contents,"images" to imagess,"address" to addresss))



        }



    }

    override fun dataObservese() {

        viewmodel.onlusc.observe(this, Observer {
            if(it.code==1){



                    list.add(0,ZaoShuju(1,it.data.url))
                    Log.e("上传图片的半路径",it.data.url)
                    binding.review.layoutManager=
                        GridLayoutManager(this, 3)
                    fabiada= FabuAdapter(this,list,this)
                    binding.review.adapter= fabiada

                    Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show()




            }


        })

        viewmodel.onfabudt.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                finish()

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()


            }


        })
    }

    override fun onItemClick(position: Int, uri: ZaoShuju) {

        if(uri.leixing==2){
            openGallery()

        }else{
            Toast.makeText(this, "图片预览中"+"    "+list.size, Toast.LENGTH_SHORT).show()


        }



    }


    private fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                file = File(getRealPathFromURI(uri))

                val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                viewmodel.onluying(body)
            }
        }
    }

    private fun getRealPathFromURI(contentUri: Uri): String {
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = contentResolver.query(contentUri, projection, null, null, null)
        val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor?.moveToFirst()
        val result = cursor?.getString(column_index!!)
        cursor?.close()
        return result!!
    }
    companion object {
        private const val GALLERY_REQUEST_CODE = 1001
    }


}
data class ZaoShuju(
    var leixing:Int,
    var tupian:String
)